﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Svg.Transforms;
using Svg;
using System.IO;
using System.Xml;
using System.Drawing;

namespace HumanFace
{
    public static class SketchHelper
    {
        public static SvgTransform GetSvgTransform(SvgTransformCollection transforms, Type type)
        {
            return transforms.Find(
                    delegate(SvgTransform item)
                    {
                        return item.GetType() == type;
                    });
        }
        public static void Scale(float x, float y, SvgTransformCollection transforms)
        {
            SvgScale scale = SketchHelper.GetSvgTransform(transforms, typeof(SvgScale)) as SvgScale;
            if (scale == null)
                transforms.Add(new SvgScale(x, y));
            else
            {
                scale.X *= x;
                scale.Y *= y;
            }
        }
        public static void Translate(float x, float y, SvgTransformCollection transforms)
        {
            SvgTranslate translate = SketchHelper.GetSvgTransform(transforms, typeof(SvgTranslate)) as SvgTranslate;
            if (translate == null)
                transforms.Add(new SvgTranslate(x, y));
            else
            {
                translate.X += x;
                translate.Y += y;
            }
        }
        public static SvgDocument ConvertTo(string svg)
        {
            if (svg == null)
                return null;
            else
            {
                using (StringReader str = new StringReader(svg))
                using (XmlTextReader xml = new XmlTextReader(str))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(xml);
                    return SvgDocument.Open(doc);
                }
            }
        }
        public static string ConvertTo(SvgDocument svgDocument)
        {
            if (svgDocument == null)
                return null;
            else
            {
                using (StringWriter str = new StringWriter())
                using (XmlTextWriter xml = new XmlTextWriter(str))
                {
                    svgDocument.WriteElement(xml);
                    return str.ToString();
                }
            }
        }
        public static PointF CalculateCoordinates(PointF templateNode1,PointF templateNode2,PointF templateMiddlePoint, PointF node1, PointF node2)
        {
            float ratioX = (templateMiddlePoint.X - templateNode1.X) / (templateNode2.X - templateNode1.X);
            float ratioY = (templateMiddlePoint.Y - templateNode1.Y) / (templateNode2.Y - templateNode1.Y);
            return CalculateCoordinates(ratioX,ratioY,node1,node2);
        }
        public static PointF CalculateCoordinates(float ratioX, float ratioY, PointF node1, PointF node2)
        {
            return new PointF(ratioX * (node2.X - node1.X) + node1.X, ratioY * (node2.Y - node1.Y) + node1.Y);
        }
    }
}
